package main

import (
	"fmt"
	lib "im/libs"
	protos "im/pb"
)

func gatewayHandle(t *(lib.Task)) {
	defer t.Client.Close()
	for {
		req,err := lib.ReadRsp(t.Client)
		if err != nil {
			break
		}
		fmt.Printf("recv %v", req)
		switch req.Command {
		case protos.Command_AUTH:
			fmt.Println("send  auth")
			r:=&protos.Response{Command:protos.Command_AUTH}
			r.Host = "127.0.0.1"
			r.Port = 1001
			rsp, _ := lib.EncodeRsp(r)
			t.Client.Write(rsp)
		case protos.Command_CHAT:
			fmt.Println("send  chat")
			r:=&protos.Response{Command:protos.Command_CHAT}
			r.Host = "127.0.0.1"
			r.Port = 1002
			rsp, _ := lib.EncodeRsp(r)
			t.Client.Write(rsp)
		case protos.Command_PING:
			r:=&protos.Response{Command:protos.Command_PONG}
			rsp, _ := lib.EncodeRsp(r)
			t.Client.Write(rsp)
		default:
			r := &protos.Response{Command: protos.Command_SEND_MSG,Errcode: protos.ErrCode_SERVER_ERR}
			rsp, _ := lib.EncodeRsp(r)
			t.Client.Write(rsp)
		}
	}
}

func main() {
	s, _ := lib.NewServer("127.0.0.1", 1000, 10, gatewayHandle)
	s.Start()
}
